package yxy.game.pm2.gm.url.mergearea;

import java.util.Collection;
import java.util.Map;

import org.joda.time.DateTime;

import yxy.cherry.data.bean.acttaskrankreward;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.active.module.ActType;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.rank.ActiveRushModule;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

/**
 * 合服冲榜排行榜处理
 * 结算旧排行榜，新榜不处理
 * @author Dell
 *
 */
public class MergeAreaHandle_冲榜排行榜 implements MergeAreaHandle {

	@Override
	public void handle(AreaBean newAreaBean, Collection<AreaBean> areas) {
		
//		冲榜_金币兑换(301),//金币兑换
//		冲榜_副本关卡(302),//副本关卡
//		冲榜_剿匪次数(303),//剿匪次数
//		冲榜_快速作战(304),//快速作战
		
		//旧榜发奖
		dealReward(areas);
			
	}

//	private <T extends PlayerRankModule> void merge(AreaBean newAreaBean, Collection<AreaBean> areas, Class<T> clazz) {
//		try {
//			T newRank = (T) clazz.getDeclaredConstructor(AreaBean.class).newInstance(newAreaBean);
//			for (AreaBean areaBean : areas) {
//				T rank = (T) clazz.getDeclaredConstructor(AreaBean.class).newInstance(areaBean);
//				newRank.importData(rank);
//			}
//		} catch (Exception e) {
//			System.err.println(clazz.getName() + " failed!!!!");
//		}
//	}
	
	private void dealReward(Collection<AreaBean> areas) {

		for (AreaBean areaBean : areas) {
			activeControl sai = activeControl.get(areaBean.getAreaId(), ActType.冲榜);
			if (sai == null) {
				continue;
			}

			ActiveRushModule module = ActiveRushModule.get(areaBean.getAreaId());
			if (module == null) {
				continue;
			}

			// 已经领取奖励了
			if (module.isAward()) {
				continue;
			}

			int type = module.getRewardtype();

			System.err.println(areaBean.getAreaId() + "区冲榜" + type + "发排名奖励了！！！！" + DateTime.now().toString());

			Map<String, acttaskrankreward> all = ActDataCenter.getAllData(acttaskrankreward.class, areaBean.getAreaId());

			for (acttaskrankreward rankdata : all.values()) {
				Collection<RankEntry> rankEntries = RankRushHandle.range(areaBean.getAreaId(), type,
						rankdata.getRankingmin(), rankdata.getRankingmax());

				Collection<Currency> rewards = Currency.create(rankdata.getRewardtype(), rankdata.getRewardid(),
						rankdata.getRewardnum());
				int rank = rankdata.getRankingmin();

				for (RankEntry entry : rankEntries) {
					String playerCode = entry.getKey();

					// 发邮件
					String title = getTitle(type);
					String content = getMsg(type, rank);
					new MailHandle().sendMail(playerCode, MailTypeEnum.Mail, title, content, CCReason.活动_冲榜, rewards);
					rank++;
				}

			}

			module.setAward(true);
			module.set();

			System.err.println(areaBean.getAreaId() + "区冲榜" + type + "排名奖励了发完了！！！！" + DateTime.now().toString());

		}
	}
	
	private String getTitle(int type) {
		if(type == RankRushEnum.冲榜_剿匪次数.getType()) {
			return GameString.冲榜活动剿匪排行奖励奖_title().getText();
		}
		
		if(type == RankRushEnum.冲榜_快速作战.getType()) {
			return GameString.冲榜活动扫荡排行奖励奖_title().getText();
		}
		
		if(type == RankRushEnum.冲榜_金币兑换.getType()) {
			return GameString.冲榜活动金币排行奖励奖_title().getText();
		}
		
		if(type == RankRushEnum.冲榜_副本关卡.getType()) {
			return GameString.冲榜活动副本排行奖励奖_title().getText();
		}
		return "";
	}
	
	private String getMsg(int type, int rank) {
		if(type == RankRushEnum.冲榜_剿匪次数.getType()) {
			return GameString.冲榜活动剿匪排行奖励奖_msg(rank).getText();
		}
		
		if(type == RankRushEnum.冲榜_快速作战.getType()) {
			return GameString.冲榜活动扫荡排行奖励奖_msg(rank).getText();
		}
		
		if(type == RankRushEnum.冲榜_金币兑换.getType()) {
			return GameString.冲榜活动金币排行奖励奖_msg(rank).getText();
		}
		
		if(type == RankRushEnum.冲榜_副本关卡.getType()) {
			return GameString.冲榜活动副本排行奖励奖_msg(rank).getText();
		}
		return "";
	}
}
